package cn.lili.modules.search.serviceimpl;

import cn.hutool.json.JSONUtil;
import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.common.utils.BeanUtil;
import cn.lili.elasticsearch.BaseElasticsearchService;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsParamsDTO;
import cn.lili.modules.goods.service.CategoryService;
import cn.lili.modules.goods.service.GoodsService;
import cn.lili.modules.goods.service.GoodsSkuService;
import cn.lili.modules.promotion.entity.dos.BasePromotions;
import cn.lili.modules.promotion.entity.dos.PromotionGoods;
import cn.lili.modules.promotion.service.PromotionService;
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
import cn.lili.modules.search.service.EsGoodsIndexService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.update.UpdateRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * ES空实现类
 *
 * @author moxingjian
 * @version 1.0
 * @date 2023-10-18 16:29
 * <p>
 * 版本更新：
 * 更新版本号    更新人      更新时间      更新内容
 */
@Slf4j
@Service
public class EsGoodsIndexServiceNewImpl extends BaseElasticsearchService implements EsGoodsIndexService {


    @Resource
    private GoodsService goodsService;
    @Resource
    private GoodsSkuService goodsSkuService;
    @Resource
    private PromotionService promotionService;
    @Resource
    private CategoryService categoryService;

    @Override
    public void init() {

    }

    @Override
    public Map<String, Long> getProgress() {
        return null;
    }

    @Override
    public void initIndex() {

    }

    @Override
    public void addIndex(EsGoodsIndex goods) {

    }

    @Override
    public void addIndex(List<EsGoodsIndex> goods) {

    }

    @Override
    public void updateIndex(EsGoodsIndex goods) {

    }

    @Override
    public void updateIndex(String id, EsGoodsIndex goods) {

    }

    @Override
    public void updateIndex(Map<String, Object> queryFields, Map<String, Object> updateFields) {

    }

    @Override
    public void updateBulkIndex(List<EsGoodsIndex> goodsIndices) {

    }

    @Override
    public void deleteIndex(Map<String, Object> queryFields) {

    }

    @Override
    public void deleteIndexById(String id) {

    }

    @Override
    public void deleteIndexByIds(List<String> ids) {

    }

    @Override
    public void initIndex(List<EsGoodsIndex> goodsIndexList, boolean regeneratorIndex) {

    }

    @Override
    public UpdateRequest updateEsGoodsIndexPromotions(String id, BasePromotions promotion, String key) {
        return null;
    }

    @Override
    public void updateEsGoodsIndexPromotions(List<String> ids, BasePromotions promotion, String key) {

    }

    @Override
    public void updateEsGoodsIndexByList(List<PromotionGoods> promotionGoodsList, BasePromotions promotion, String key) {

    }

    @Override
    public void updateEsGoodsIndexAllByList(BasePromotions promotion, String key) {

    }

    @Override
    public void deleteEsGoodsPromotionByPromotionKey(List<String> skuIds, String promotionsKey) {

    }

    @Override
    public void deleteEsGoodsPromotionByPromotionKey(String promotionsKey) {

    }

    @Override
    public void cleanInvalidPromotion() {

    }

    @Override
    public EsGoodsIndex findById(String id) {
        return null;
    }

    @Override
    public Map<String, Object> getPromotionMap(String id) {
        return null;
    }

    @Override
    public List<String> getPromotionIdByPromotionType(String id, PromotionTypeEnum promotionTypeEnum) {
        return null;
    }

    @Override
    public EsGoodsIndex getResetEsGoodsIndex(GoodsSku goodsSku, List<GoodsParamsDTO> goodsParamDTOS) {
        return getGoodsIndexById(goodsSku.getId());
    }

    @Override
    public EsGoodsIndex getGoodsIndexById(String goodsSkuId) {
        EsGoodsIndex goodsIndex = new EsGoodsIndex();
        // 仍旧为空，则查询数据库
        GoodsSku goodsSkuTemp = goodsSkuService.getById(goodsSkuId);
        // 获取产品信息
        Goods goods = goodsService.getById(goodsSkuTemp.getGoodsId());
        // 产品信息复制
        BeanUtil.copyProperties(goods, goodsIndex);
        // 规格复制
        BeanUtil.copyProperties(goodsSkuTemp, goodsIndex);
        // 促销信息
        Map<String, Object> goodsCurrentPromotionMap = promotionService.getGoodsSkuPromotionMap(goodsIndex.getStoreId(), goodsIndex.getId());
        goodsIndex.setPromotionMapJson(JSONUtil.toJsonStr(goodsCurrentPromotionMap));
        // 设置分类名称
        List<String> categoryNameList = categoryService.getCategoryNameByIds(
                Arrays.stream(goodsIndex.getCategoryPath().split(",")).toList());
        StringBuilder categoryNames = new StringBuilder();
        for (int i = 0; i < categoryNameList.size(); i++) {
            categoryNames.append(categoryNameList.get(i));
            if (i < categoryNameList.size() - 1) {
                categoryNames.append(",");
            }
        }
        goodsIndex.setCategoryNamePath(categoryNames.toString());
        return goodsIndex;
    }
}
